//
//  MNDNoticeViewController.m
//  minidai
//
//  Created by Steven.Yung on 2017/5/10.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDNoticeViewController.h"
#import "MNDRechargeGuideViewController.h"
#import "MNDBankModel.h"
#import "NSString+BankCardFormatter.h"

@interface MNDNoticeViewController ()

/*! 提示标题 如网银充值 支付宝充值 */
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
/*! 说明文字 */
@property (weak, nonatomic) IBOutlet UILabel *explainLabel;
/*! 收款方户名 */
@property (weak, nonatomic) IBOutlet UILabel *payeeBankLabel;
/*! 收款方银行卡号 */
@property (weak, nonatomic) IBOutlet UILabel *payeeBankcardLabel;
/*! 复制按钮 */
@property (weak, nonatomic) IBOutlet UIButton *bankcardCopydBtn;
/*! 收款方开户行 */
@property (weak, nonatomic) IBOutlet UILabel *payeeBanknameLabel;
/*! 银行logo */
@property (weak, nonatomic) IBOutlet UIImageView *jxBankLogo;

@property (strong, nonatomic) MNDBankModel *bankModel;

@end

@implementation MNDNoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.isApliPay) {
        self.title = @"支付宝转账";
        self.titleLabel.text = @"支付宝转账";
        self.explainLabel.text = @"您可以使用您的银行卡, 通过支付宝转账的方式将资金充值到您的江西银行存管账户, 账户信息如下:";
        [self initialBarButtonItems];
        
    } else {
        self.title = @"网银转账";
        self.titleLabel.text = @"网银转账";
        self.explainLabel.text = @"您可以使用您的银行卡, 通过转账（柜台、网银、手机银行）的方式将资金充值到您的江西银行存管账户, 账户信息如下:";
    }
    
    self.bankcardCopydBtn.layer.cornerRadius = 3.0;
    self.bankcardCopydBtn.layer.borderWidth = 1.0;
    self.bankcardCopydBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    [self.bankcardCopydBtn bk_addEventHandler:^(id sender) {
        
//        复制内容
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = self.payeeBankcardLabel.text;
        [NetworkProgressHUD showHudMsg:@"银行卡号已复制到剪切板"];
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadBankInfo];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initialBarButtonItems {
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = kScreenWidth < 414? -10 : -15;
    
    UIImage *itemImage = [UIImage imageNamed:@"notice_help_icon"];
    UIBarButtonItem *helpItem = [[UIBarButtonItem alloc] initWithImage: itemImage
                                                                 style: UIBarButtonItemStylePlain
                                                                target: self
                                                                action: @selector(helpBtnAction:)];
    helpItem.tintColor = kColorOrange;
    
    self.leftBarTintColor = kColorOrange;
    
    if (@available(iOS 11.0, *)) {
        self.navigationItem.rightBarButtonItem = helpItem;
    } else {
        self.navigationItem.rightBarButtonItems = @[spaceItem, helpItem];
    }
}

- (void)loadBankInfo {
    
    WeakSelf(self);
    
    [self.view beginLoading];
    
    [APIManager sendRequestWithPath:API_GET_BANK_ACCOUNT parameters:[MNDSign signedParam] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD hideHud];
            weakself.bankModel = [MNDBankModel mj_objectWithKeyValues:responseModel.data];
            [weakself configBankInfo];
            
        } else {
            NSDictionary *dict = responseModel.msg;
            if (dict && dict.count > 0) {
                [NetworkProgressHUD showHudMsg:dict[@"error"]];
                
            } else {
                [NetworkProgressHUD showHudMsg:@"获取数据失败"];
            }
        }
        [weakself.view endLoading];
    } onFailure:^(NSError *error) {
        [weakself.view endLoading];
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

- (void)configBankInfo {
    self.payeeBankcardLabel.text = [self.bankModel.jxBankaccount.userAmountId formatterBankCardNum];
    
    self.payeeBankLabel.text = [NSString stringWithFormat:@"收款方户名: %@", self.bankModel.userAmount.userRealname];
}

- (void)helpBtnAction:(id)sender {
    
    [self performSegueWithIdentifier:[MNDRechargeGuideViewController description] sender:nil];
}

- (void)backAction :(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
